const resources = {};
const resourcesGroup = {};
const g_StageImgRes = {};

module.exports = {
    //加载资源
    loadResources: function (groupName, completeCallback, progressCallback) {
        this.loadResourcesArray(resourcesGroup[groupName], completeCallback, progressCallback);
    },
    loadResourcesArray: function (urls, completeCallback, progressCallback) {
        cc.loader.loadResArray(urls, progressCallback, (error, assets) => {
            if (!error) {
                for (let i = 0; i < assets.length; i++) {
                    resources[urls[i]] = assets[i];
                }
                completeCallback();
            } else {
                cc.error(error);
                cc.error('加载资源组失败，两秒后重试！');
                setTimeout(() => {
                    this.loadResourcesArray(urls, completeCallback, progressCallback);
                }, 2000);
            }
        });
    },
    get: function (url) {
        if (!resources[url]) {
            cc.error('您必须先加载这个资源', url);
        }
        return resources[url];
    },
    LoadImgResources:function(urls,completeCallback, progressCallback)
    {
        var loadArrayFinish = function(error,resource)
        {
            if(!resource) 
            {
                cc.error("Load loadArray Error:"+error);
                if(completeCallback)completeCallback(null,error);
                return ;
            }
            for(var idx=0;idx <urls.length;idx++)
            {
                g_StageImgRes[urls[idx]]=resource[idx];
            }
            if(completeCallback)completeCallback(resource,error);
        }
        cc.loader.loadResArray(urls,cc.spriteFrame,progressCallback,loadArrayFinish.bind(this));
    },
    getImgResource:function(url)
    {
        return g_StageImgRes[url];
    },

    getInstantiate: function (url) {
        return cc.instantiate(this.get(url));
    },
    addPrefabToCanvas: function (url, parent = cc.find('Canvas'), pos = { x: 0, y: 0 }) {
        let p = this.getInstantiate(url);
        p.x = pos.x;
        p.y = pos.y;
        parent.addChild(p);
        return p;
    },
    loaderScene: function (name) {
        cc.log('======>>跳转场景:' + name);
        let mainScene = cc.find('Canvas/Main');
        this.beDestroy(mainScene);
        let loadding = cc.find('Canvas/Loadding');
        let progre = cc.find('UI-ProgressBar', loadding)
        loadding.active = true;
        progre.js.setPercent(0);
        cc.loader.loadRes('./scene/' + name + 'Res', (error, json) => {
            let preArr = json.json;
            this.loadResourcesArray(preArr, () => {
                var scene = this.getInstantiate(preArr[0]);
                mainScene.addChild(scene);
                loadding.active = false;
            }, (OKCount, sumCount) => {
                progre.js.setPercent((OKCount / sumCount).toFixed(2));
                // cc.log(OKCount, sumCount);
            });
        });
    },
    beDestroy: function (node) {
        var obj;
        while (node.children.length) {
            obj = node.children[0];
            obj.removeFromParent();
            obj.destroy();
        }
    },
}